﻿/*!	@file	r_thread_server.h
	@brief	スレッド管理クラス
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _R_THREAD_SERVER_H_
#define _R_THREAD_SERVER_H_

#include "../rtti/r_rtti_class_base.h"
#include "../util/r_util_singleton.h"
#include <vector>

namespace r{
	namespace thread{
		class PoolThread;
		namespace profile{
			class Server;
		}
	}
}

namespace r{
	namespace thread{
		//! スレッド管理クラス
		/*!
		*/
		class Server : public r::rtti::ClassBase
		{
			RRTTI_REFLECTION_DECLARE
			RSINGLETONTGET_DECLARE ( Server )
		protected:
			//! コンストラクタ
			/*!
			*/
			Server();
			//! デストラクタ
			/*!
			*/
			virtual	~Server();
		public:
			// システム呼び出し
			//! 同期処理
			/*!
			*/
			virtual	void			sync() {}
			//! プロファイラを開始する
			/*!
			*/
			void					begineProfile();
			//! プロファイラを終了する
			/*!
			*/
			void					endProfile();
		public:
			//! スレッドインデックスを取得
			/*!
			*/
			virtual	r_ui32			getThreadIndex() const {
				return 0;
			}
			//! スレッドに割り当てられたプロファイラを取得する
			/*!
			*/
			r::thread::profile::Server*	getProfileServer() const;
			r::thread::profile::Server*	getProfileServer ( r_ui32 index ) const;
		protected:
			//! スレッドIDを設定する
			virtual	void			setThreadIndex ( r_si32 index ) {}
			//! プロファイラの作成を行う
			/*! この関数を呼び出すまでにgetThreadNum()は正常なスレッドの数を返す必要があります。
			*/
			void					initProfiler();
		protected:
			typedef std::vector<PoolThread*>	threadpool_array;
			typedef std::vector<r::thread::profile::Server*>	profile_array;

			threadpool_array		poolThreads;
			profile_array			profiles;
		private:
			//! プロファイラの削除
			/*!
			*/
			void					deleteProfiler();
		};
	}
}

#endif // _R_THREAD_SERVER_H_
